/**
 * 读取数据流，有open,data,close,end,readable等函数监听
 * data与readable互斥，且readable优先级高于data
 * pause可以暂停读取，resume可以开启暂停
 * 只有数据被消费了才会触发close和end，也就是有data或者readable
 */

const fs = require('fs')

const bufferArray = []
const rs = fs.createReadStream('./res/test.txt', {
    highWaterMark: 2
})


rs.on('data', (chunk) => {
    console.log(chunk.toString())
    rs.pause()
    setTimeout(() => {
        rs.resume()
    }, 1000)
})

rs.on('open', () => {
    console.log('开始读取数据')
})

rs.on('end', () => {
    console.log(bufferArray)
    const result = Buffer.concat(bufferArray)
    console.log(result.toString())
    console.log('读取完毕')
})

rs.on('close', () => {
    console.log('关闭读取')
})


// rs.on('readable', () => {
//     const buf = rs.read()
//     if(buf !== null) {
//         bufferArray.push(buf)
//     }
// })